﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Webdiyer.WebControls.Mvc;

namespace HouseAgencySite.Utils
{
    public static class AgencyPagerHelper
    {

        #region Ajax分页类封装
        /// <summary>
        /// Ajax分页
        /// </summary>
        /// <param name="ajax">扩展类，不需要传值</param>
        /// <param name="pagedList">pagedList</param>
        /// <param name="domId">需要显示数据的div，ID</param>
        /// <returns>返回分页后的页码条</returns>
        public static MvcHtmlString AjaxPager(this AjaxHelper ajax, IPagedList pagedList, string domId)
        {
            if (pagedList == null)
            {
                return MvcHtmlString.Create(string.Empty);
            }
            int totalCount = pagedList.TotalItemCount;
            int pageCount = (int)Math.Ceiling((double)pagedList.TotalItemCount / (double)pagedList.PageSize);
            string strName = totalCount == 1500 ? "前" : "共";
            return MvcHtmlString.Create(PagerHelper.Pager(ajax, pagedList, new PagerOptions { PageIndexParameterName = "pageIndex", ShowFirstLast = false, AutoHide = false, ShowPageIndexBox = false, CssClass = "pages-inner", ShowGoButton = false, CurrentPagerItemWrapperFormatString = "<span class=\"mvcPager-current\">{0}</span>" }, new MvcAjaxOptions { UpdateTargetId = domId, HttpMethod = "Post", OnBegin = "ajaxpageronbegin()", OnSuccess = "loadSuccess()" }).ToHtmlString() + "<span class=\"all\">" + strName + pageCount + "页</span><span class=\"all\">" + strName + pagedList.TotalItemCount + "条&nbsp;|&nbsp;</span><div id=\"loadimg\" style=\"display:none;position:fixed;z-index:999;top:50%;left:50%\"><img src=\"/Content/images/default/loading-big.gif\" style=\"width:65px;height:65px;\" /></div><script>function ajaxpageronbegin(){if(this.pagercustomerFun){this.pagercustomerFun()};$('#loadimg').show();};function loadSuccess(){if(this.pageOnSuccessFun){pageOnSuccessFun()};}</script>");
        }

        /// <summary>
        ///  AjaxForm分页
        /// </summary>
        /// <param name="ajax">扩展类，不需要传值</param>
        /// <param name="pagedList">pagedList</param>
        /// <param name="domId">需要显示数据的div，ID</param>
        /// <param name="subFormId">表单id</param>
        /// <returns>返回分页后的页码条</returns>
        public static MvcHtmlString AjaxPager(this AjaxHelper ajax, IPagedList pagedList, string domId, string subFormId)
        {
            if (pagedList == null)
            {
                return MvcHtmlString.Create(string.Empty);
            }

            int totalCount = pagedList.TotalItemCount;
            int pageCount = (int)Math.Ceiling((double)pagedList.TotalItemCount / (double)pagedList.PageSize);
            string strName = totalCount == 1500 ? "前" : "共";
            return MvcHtmlString.Create(PagerHelper.Pager(ajax, pagedList, new PagerOptions { PageIndexParameterName = "pageIndex", ShowFirstLast = false, AutoHide = false, ShowPageIndexBox = false, CssClass = "pages-inner", ShowGoButton = false, CurrentPagerItemWrapperFormatString = "<span class=\"mvcPager-current\">{0}</span>" }, new MvcAjaxOptions { UpdateTargetId = domId, HttpMethod = "Post", DataFormId = subFormId, OnBegin = "ajaxpageronbegin()", OnSuccess = "loadSuccess()" }).ToHtmlString() + "<span class=\"all\">" + strName + pageCount + "页</span><span class=\"all\">" + strName + pagedList.TotalItemCount + "条&nbsp;|&nbsp;</span><span id=\"timeCount\" class=\"dib\"></span><div id=\"loadimg\" style=\"display:none;position:fixed;z-index:999;top:50%;left:50%\"><img src=\"/Content/images/default/loading-big.gif\" style=\"width:65px;height:65px;\" /></div><script>function ajaxpageronbegin(){if(this.pagercustomerFun){pagercustomerFun()};$('#loadimg').show();if('undefined' != typeof setPageStartTime){setPageStartTime();};if('undefined' != typeof other){other.setPageStartTime();};};function loadSuccess(){if(this.pageOnSuccessFun){pageOnSuccessFun()};if('undefined' != typeof setPageEndTime){setPageEndTime();};if('undefined' != typeof other){other.setPageEndTime();};};</script>");
        }


        public static MvcHtmlString AjaxPager(this AjaxHelper ajax, IPagedList pagedList, string domId, string subFormId, string paraDesc)
        {
            if (pagedList == null)
            {
                return MvcHtmlString.Create(string.Empty);
            }
            int totalCount = pagedList.TotalItemCount;
            int pageCount = (int)Math.Ceiling((double)pagedList.TotalItemCount / (double)pagedList.PageSize);
            string strName = totalCount == 1500 ? "前" : "共";
            return MvcHtmlString.Create(PagerHelper.Pager(ajax, pagedList,
                new PagerOptions { PageIndexParameterName = paraDesc, AutoHide = false, ShowPageIndexBox = false, HorizontalAlign="bottom", ShowFirstLast = false, CssClass = "pages-inner", ShowGoButton = false, CurrentPagerItemWrapperFormatString = "<span class=\"mvcPager-current\">{0}</span>" },
                new MvcAjaxOptions { UpdateTargetId = domId, HttpMethod = "Post", DataFormId = subFormId, OnBegin = "ajaxpageronbegin()", OnSuccess = "loadSuccess()" }).ToHtmlString() + "<span class=\"all\">" + strName + pageCount + "页</span><span class=\"all\">" + strName + pagedList.TotalItemCount + "条&nbsp;|&nbsp;</span><div id=\"loadimg\" style=\"display:none;position:fixed;z-index:999;top:50%;left:50%\"><img src=\"/Content/images/default/loading-big.gif\" style=\"width:65px;height:65px;\" /></div><script>function ajaxpageronbegin(){if(this.pagercustomerFun){pagercustomerFun()};$('#loadimg').show();};function loadSuccess(){if(this.pageOnSuccessFun){pageOnSuccessFun()};}</script>");
        }

        #endregion

        #region Html分页类封装

        /// <summary>
        /// 页面刷新分页
        /// </summary>
        /// <param name="html">扩展类，不需要传值</param>
        /// <param name="pagedList">pagedList</param>
        /// <returns>MvcHtmlString</returns>
        public static MvcHtmlString HtmlPager(this HtmlHelper html, IPagedList pagedList)
        {
            if (pagedList == null)
            {
                return MvcHtmlString.Create(string.Empty);
            }

            int pageCount = (int)Math.Ceiling((double)pagedList.TotalItemCount / (double)pagedList.PageSize);

            return MvcHtmlString.Create(PagerHelper.Pager(html, pagedList, new PagerOptions { PageIndexParameterName = "pageIndex", ShowPageIndexBox = false, CssClass = "pages-inner", ShowGoButton = false, CurrentPagerItemWrapperFormatString = "<span class=\"mvcPager-current\">{0}</span>" }, new MvcAjaxOptions { HttpMethod = "Post", OnBegin = "ajaxpageronbegin()" }).ToHtmlString() + "<span class=\"all\">共" + pageCount + "页</span><span class=\"all\">共" + pagedList.TotalItemCount + "条</span>");
        }

        #endregion
    }
}